package cn.fl.ref.account.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.api.ISysRegionApiService;
import cmm.comm.facade.framework.dto.SysRegionDTO;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.customer.service.ICstBscInfoService;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.ref.account.service.IFncGroupCompanyAccountService;
import cn.fl.ref.account.transform.FncGroupCompanyAccountVOTransform;
import cn.fl.ref.account.vo.FncGroupCompanyAccountVO;
import fl.flow.facade.flow.api.IFlowApiService;
import fl.flow.facade.flow.dto.FlowStartParamsDTO;
import fl.ref.account.api.IFncGroupCompanyAccountApiService;
import fl.ref.account.api.IFncGroupCompanyApiService;
import fl.ref.account.condition.FncGroupCompanyAccountQueryCondition;
import fl.ref.account.constant.FncGroupCompanyConstants;
import fl.ref.account.dto.FncGroupCompanyAccountDTO;
import fl.warrant.facade.archive.condition.ArcBorrowMainQueryCondition;

/**
 * @Description: 集团公司账户
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get*
 *               批量：batch* 查询集合：search*
 */
@Service
public class FncGroupCompanyAccountServiceImpl implements IFncGroupCompanyAccountService {

	private static final Logger logger = LoggerFactory.getLogger(IFncGroupCompanyAccountService.class);

	@Autowired
	private IFncGroupCompanyAccountApiService fncGroupCompanyAccountApiService;
	@Autowired
	private IFlowApiService flowApiService;
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;
	@Autowired
	private ICstBscInfoService cstBscInfoService;
	@Autowired
	private IFncGroupCompanyApiService fncGroupCompanyApiService;
	@Autowired
	private ISysRegionApiService sysRegionApiService;

	@Override
	public FncGroupCompanyAccountVO selectById(Long id) throws BaseException {
		return FncGroupCompanyAccountVOTransform.toVO(fncGroupCompanyAccountApiService.selectById(id));
	}

	@Override
	public Long add(FncGroupCompanyAccountVO entity) throws BaseException {
		return this.fncGroupCompanyAccountApiService.add(FncGroupCompanyAccountVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id) throws BaseException {
		return this.fncGroupCompanyAccountApiService.removeById(id);
	}

	@Override
	public boolean update(FncGroupCompanyAccountVO entity) throws BaseException {
		return this.fncGroupCompanyAccountApiService.update(FncGroupCompanyAccountVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException {
		Pager pager = this.fncGroupCompanyAccountApiService.searchListPage(page);

		// 设置显示内容
		if (pager != null) {
			List<FncGroupCompanyAccountDTO> resultList = (List<FncGroupCompanyAccountDTO>) pager.getResultList();
			if (CollectionUtils.isNotEmpty(resultList)) {

				// 账户状态列表
				List<KeyValueVO> accountStatusList = sysDictionaryApiService.getKvList(
						FncGroupCompanyConstants.FNC_ACCOUNT_STATUS, CurrentThreadContext.getCurrentSysCd(),
						CurrentThreadContext.getCurrentOrgCd());

				// 账户性质(数据字典)列表
				List<KeyValueVO> bankNatureList = sysDictionaryApiService.getKvList(
						FncGroupCompanyConstants.FNC_ACCOUNT_BANK_NATURE, CurrentThreadContext.getCurrentSysCd(),
						CurrentThreadContext.getCurrentOrgCd());

				// 开户银行(数据字典)
				List<KeyValueVO> bankList = sysDictionaryApiService.getKvList(FncGroupCompanyConstants.FNC_BANK,
						CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());

				// 账户归属部门列表
				List<KeyValueVO> businessOrgList = sysDictionaryApiService.getKvList(FncGroupCompanyConstants.ORG_CD,
						CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
				
				for (FncGroupCompanyAccountDTO fncGroupCompanyAccountDTO : resultList) {

					// 设置账户状态(数据字典)
					if (CollectionUtils.isNotEmpty(accountStatusList)) {
						if (StringUtils.isNotBlank(fncGroupCompanyAccountDTO.getAccountStatus())) {
							for (KeyValueVO keyValueVO : accountStatusList) {
								if (fncGroupCompanyAccountDTO.getAccountStatus().equals(keyValueVO.getKey())) {
									fncGroupCompanyAccountDTO.setAccountStatusNm(keyValueVO.getValue());
									break;
								}
							}
						}
					}

					// 账户性质(数据字典)
					if (CollectionUtils.isNotEmpty(bankNatureList)) {
						if (StringUtils.isNotBlank(fncGroupCompanyAccountDTO.getBankNature())) {
							for (KeyValueVO keyValueVO : bankNatureList) {
								if (fncGroupCompanyAccountDTO.getBankNature().equals(keyValueVO.getKey())) {
									fncGroupCompanyAccountDTO.setBankNatureNm(keyValueVO.getValue());
									break;
								}
							}
						}
					}

					// 账户归属部门(事业部)
					if (CollectionUtils.isNotEmpty(businessOrgList)) {
						if (StringUtils.isNotBlank(fncGroupCompanyAccountDTO.getBuOrgcd())) {
							for (KeyValueVO keyValueVO : businessOrgList) {
								if (fncGroupCompanyAccountDTO.getBuOrgcd().equals(keyValueVO.getKey())) {
									fncGroupCompanyAccountDTO.setBuOrgcdNm(keyValueVO.getValue());
									break;
								}
							}
						}
					}

					// 设置开户银行(数据字典)
					if (CollectionUtils.isNotEmpty(bankList)) {
						if (StringUtils.isNotBlank(fncGroupCompanyAccountDTO.getBank())) {
							for (KeyValueVO keyValueVO : bankList) {
								if (fncGroupCompanyAccountDTO.getBank().equals(keyValueVO.getKey())) {
									fncGroupCompanyAccountDTO.setBankNm(keyValueVO.getValue());
									break;
								}
							}
						}
					}

					// 所属集团公司
					if (fncGroupCompanyAccountDTO.getGroupCompanyId() != null) {
						fncGroupCompanyAccountDTO.setFncGroupCompanyDTO(
								fncGroupCompanyApiService.selectById(fncGroupCompanyAccountDTO.getGroupCompanyId()));
					}

					// 开户地(省)
					if (StringUtils.isNotBlank(fncGroupCompanyAccountDTO.getBankProv())) {

						SysRegionDTO sysRegionDTO = sysRegionApiService.getRegionByRgnCd(
								fncGroupCompanyAccountDTO.getBankProv(), FncGroupCompanyConstants.SYS_CODE);
						if (sysRegionDTO != null) {
							fncGroupCompanyAccountDTO.setBankProv(sysRegionDTO.getRgnNm());
						}
					}

					// 开户地(市)
					if (StringUtils.isNotBlank(fncGroupCompanyAccountDTO.getBankCity())) {

						SysRegionDTO sysRegionDTO = sysRegionApiService.getRegionByRgnCd(
								fncGroupCompanyAccountDTO.getBankCity(), FncGroupCompanyConstants.SYS_CODE);
						if (sysRegionDTO != null) {
							fncGroupCompanyAccountDTO.setBankCity(sysRegionDTO.getRgnNm());
						}
					}

				}
			}
		}

		return pager;
	}

	@Override
	public List<FncGroupCompanyAccountVO> selectList(FncGroupCompanyAccountQueryCondition qc) throws BaseException {
		return FncGroupCompanyAccountVOTransform.toVOList(fncGroupCompanyAccountApiService.searchList(qc));
	}

	@Override
	public DataResultDTO addFlowStart(FlowStartParamsDTO flowStartParamsDTO) throws BaseException{
		if(StringUtils.isEmpty(flowStartParamsDTO.getCrtUsrId()) || StringUtils.isEmpty(flowStartParamsDTO.getBusinessKey())){
			BaseLogUtils.info(logger, "addFlowStart", "用户ID和业务数据主键必须要存在!",CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "用户ID和业务数据主键必须要存在!");
		}
		return this.fncGroupCompanyAccountApiService.addFlowStart(flowStartParamsDTO);
	}
	
	/**
	 * 流程列表
	 */
	public Pager getFlowListPage(Pager page) throws BaseException  {
		return this.fncGroupCompanyAccountApiService.getFlowListPage(page);
	}
}
